$(function() {

    // cache
    var productContainer = $('[data-role="productsListRow"]'),   // 列表容器
        sPage = $('[data-role="sPage"]'),  // 迷你分页容器
        prevBtn = $('[data-role="sprev"]'),// 迷你分页上一页
        nextBtn = $('[data-role="snext"]');// 迷你分页下一页
    var curPage = 1;
    var pageSize = 40;
    var selectItemParamHasGoods = false;
    var selectItemParam = {};

    var brangIdArr = [];
    var propsArr = [[], [], [], [], [], [], []];
    // 遍历删除数组的元素
    function deleteArr(arrItem, arr) {
        var index;
        for(var i = 0, len = arr.length; i < len;i++) {
            if(arr[i] == arrItem) {
                index = i;
                break;
            }
        }
        arr.splice(index, 1);
        return arr;
    }

    // 选择请求参数
    function selectData(brangIdArr, propsArr) {
        var obj = {};
        obj.brandIds = brangIdArr.join(',');

        $.each(propsArr, function(index, item) {
            if(item.length) {
                obj['props'+(index+1)] = item.join(',')
            }
        })
        return obj;
    }
    //加标签
    function add_sel($mark,$marktxt,$fid,$stxt){
        if($fid.indexOf('brandid') != -1) {
            brangIdArr.push($fid.split('-')[1]);
        } else if($fid.indexOf('prop') != -1) {
            var index = $fid.substring(5, 6);
            propsArr[index].push($fid.split('-')[1])
        }

        wsList.selectAjax(selectData(brangIdArr, propsArr));

         var $posi = $(".filter-selected-values");
         var Bmark;
         if($posi.find("span[data-label="+$mark+"]").length==0){
             Bmark = $("<span></span>").appendTo($posi);
             Bmark.addClass("filter-selected-entries").attr("data-label",$mark);
             var Bmarktxt = $("<label></label>").appendTo(Bmark);
             Bmarktxt.addClass("filter-selected-label").html($marktxt);
         }else{
            Bmark = $posi.children("span[data-label="+$mark+"]");
         }
         var newsubspan = $("<span></span>").appendTo(Bmark);
         newsubspan.addClass("filter-selected-item").attr("data-fid",$fid).html($stxt);
         var Bclose = $("<a>×</a>").appendTo(newsubspan);
         Bclose.addClass("action-delete-filter icon").attr("href","javascript:void(0);")
         Bclose.click(function(){
            del_sel($mark,$marktxt,$fid,$stxt);
         })
    };

    //减标签
    function del_sel($mark,$marktxt,$fid,$stxt){
        if($fid.indexOf('brandid') != -1) {
            brangIdArr = deleteArr($fid.split('-')[1], brangIdArr)
        } else if($fid.indexOf('prop') != -1) {
            var index = $fid.substring(5, 6);
            propsArr[index] = deleteArr($fid.split('-')[1], propsArr[index])
        }
        wsList.selectAjax(selectData(brangIdArr, propsArr));

        var del_entry = $("dl[data-label="+$mark+"]");
        var del_btn = del_entry.find("span[data-fid="+$fid+"]").eq(0);

        del_btn.removeClass("active");
        if(del_btn.parents("ul").find(".active").length==0){
                del_btn.parents("ul").find(".action-select-unlimit").parent().addClass("active");
            }

        var del_value = $(".filter-selected-values span[data-label="+$mark+"]");
        del_value.find("span[data-fid="+$fid+"]").remove();
        if(del_value.children("span").length==0){ del_value.remove();}
    };

    var wsList = {
        getList: function() {
            $.ajax({
                url: wsCommon.host + '/api/goods/list/' + wsCommon.getUrlParam('catId'),
                dataType: wsCommon.jsonType,
                data: {
                    pageSize: pageSize
                },
                success: function(res) {
                    var productsList = typeof(res.data.pager.list) != "undefined" ? res.data.pager.list : [];
                    var brandList = res.data.brandList ? res.data.brandList : [];
                    var typePropsList = res.data.typePropsList;
                    var goodsCatList = res.data.goodsCatList ? res.data.goodsCatList : [];
                    var pager = res.data.pager;

                    // goodsCatList
                    var goodsCatListTpl = '';
                    if(goodsCatList.length) {
                        goodsCatListTpl = '<dl class="filter-entries clearfix">\
                                                <dt class="filter-entries-label">分类：</dt>\
                                                <dd class="filter-entries-values">\
                                                    <ul class="attr-item">';
                    }
                    $.each(goodsCatList, function(index, item) {
                        goodsCatListTpl += '<li><span class="filter-item"><a href="./list.html?catId='+item.catId+'" class="handle action-cat-filter">'+item.catName+'</a></span></li>';
                    })
                    goodsCatListTpl += '</ul></dd></dl>';
                    $('[data-role="typePropsList"]').append(goodsCatListTpl);

                    var typePropsListTpl = '';
                    $.each(typePropsList, function(index, item) {
                        if(item.show == 1 && item.type == 0) {
                            typePropsListTpl += '<dl class="filter-entries clearfix" data-label="props'+index+'">\
                                                    <dt class="filter-entries-label">'+item.name+'：</dt>\
                                                    <dd class="filter-entries-values">\
                                                        <ul class="attr-item">';
                            item.typePropsValueList.unshift({
                                name: '不限'
                            })
                            $.each(item.typePropsValueList, function(indexDir, itemDir) {
                                if(indexDir == 0) {
                                    typePropsListTpl += '<li><span class="filter-item active" data-fid="props'+index.toString() + '-none"><a href="javascript:;" class="handle action-select-unlimit">'+itemDir.name+'</a></span></li>';
                                } else {
                                    typePropsListTpl += '<li><span class="filter-item" data-fid="props'+index.toString() + '-' + itemDir.propsValueId+'"><a href="javascript:;" class="handle action-select-filter">'+itemDir.name+'</a><a href="javascript:;" class="icon action-delete-filter">×</a></span></li>';
                                }
                            })
                            typePropsListTpl += '</ul></dd></dl>';
                        }
                    })
                    $('[data-role="typePropsList"]').append(typePropsListTpl);


                    //brandList
                    var brandLislTpl = '';
                    if(brandList.length != 0) {
                        brandLislTpl = '<dl class="filter-entries clearfix" data-label="brandid">\
                                                <dt class="filter-entries-label">品牌：</dt>\
                                                <dd class="filter-entries-values">\
                                                    <ul class="attr-item">';
                        brandList.unshift({
                            brandName: '不限'
                        })
                    }


                    $.each(brandList, function(index, item) {
                        if(index == 0) {
                            brandLislTpl += '<li><span class="filter-item active" data-fid="brandid-none"><a href="javascript:;" class="handle action-select-unlimit">'+item.brandName+'</a></span></li>';
                        } else {
                            brandLislTpl += '<li><span class="filter-item" data-fid="brandid-'+item.brandId+'"><a href="javascript:;" class="handle action-select-filter">'+item.brandName+'</a><a href="javascript:;" class="icon action-delete-filter">×</a></span></li>';
                        }
                    })
                    brandLislTpl += '</ul></dd></dl>';
                    $('[data-role="typePropsList"]').append(brandLislTpl);

                    wsList.randerList(productsList, pager);

                    //筛选条件
            		var sel_enty = $(".action-select-filter");
            		var sel_all = $(".action-select-unlimit");
            		var filter_sel = $("#filter_selected dl dd.filter-selected-values");


            		// sel_enty.click(function(){
            			// var $brand = $(this);
            			// var $mark = $(this).parents("dl").attr("data-label");
            			// var $marktxt = $(this).parents("dl").find("dt.filter-entries-label").eq(0).html();
            			// var $fid = $(this).parent().attr("data-fid");
            			// var $stxt = $(this).html();

            			sel_enty.on('click', function(){
                            var $brand = $(this);
                			var $mark = $(this).parents("dl").attr("data-label");
                			var $marktxt = $(this).parents("dl").find("dt.filter-entries-label").eq(0).html();
                			var $fid = $(this).parent().attr("data-fid");
                			var $stxt = $(this).html();

            				if($brand.parent("span").hasClass("active")){
            				    return false;
            				}else{
            					$brand.parent("span").addClass("active");
            					add_sel($mark,$marktxt,$fid,$stxt);//加标签
            					if($brand.parents("ul").find(".action-select-unlimit").parent().hasClass("active")){
            						$brand.parents("ul").find(".action-select-unlimit").parent().removeClass("active");
            					}
            				}
            			})

            			sel_enty.next(".action-delete-filter").click(function(){
                			var $mark = $(this).parents("dl").attr("data-label");
                			var $marktxt = $(this).parents("dl").find("dt.filter-entries-label").eq(0).html();
                			var $fid = $(this).parent().attr("data-fid");
                			var $stxt = $(this).html();
            				  del_sel($mark,$marktxt,$fid,$stxt);
            			});

            			sel_enty.parents("ul").find(".action-select-unlimit").click(function(){
                            var $fid = $(this).parents('span').attr('data-fid');
                            if($fid.indexOf('brandid') != -1) {
                                brangIdArr = [];
                            } else if($fid.indexOf('prop') != -1) {
                                var index = $fid.substring(5, 6);
                                propsArr[index] = [];
                            }
                            wsList.selectAjax(selectData(brangIdArr, propsArr));

                			var $mark = $(this).parents("dl").attr("data-label");
            				 if($(this).parent().hasClass("active")){
            				     return false;
            				   }else{
            					  $(this).parents("ul").find(".active").removeClass("active");
            					  $(this).parent().addClass("active");
            					  $(".filter-selected-values span[data-label="+$mark+"]").remove();
            				   }
            			});

            			//清空筛选条件
            			$(".filter-clean").click(function(){
            			    $(".filter-selected-values").empty();
            				$(".filter-entries-values").find(".action-select-filter").parent().removeClass("active");
            				$(".filter-entries-values").find(".action-select-unlimit").parent().addClass("active");
                            brangIdArr = [];
                            propsArr = [[], [], [], [], [], [], []];
                            wsList.selectAjax(selectData(brangIdArr, propsArr));
            			});

            			//更多条件
            			$(".action-unfold-entries").click(function(){
            				var btnspan =  $(this).find("span");
            				var btnspantxt = btnspan.html();
            				var btnicon =  $(this).find("i");
            				var btnicontxt = btnicon.html();
            				btnspan.html(btnspan.attr("data-toggle"));
            				btnspan.attr("data-toggle",btnspantxt);
            				btnicon.html(btnicon.attr("data-toggle"));
            				btnicon.attr("data-toggle",btnicontxt);

            			    $(this).parents("dl").find(".filter-item-hide").toggleClass("filter-item-show");
            			})
                    // });

                    // 初始化分页
                    curPage = 1;
                    wsList.initPaging(pager.pages, pager.total, res)
                },
                error: function() {
                    // console.log('queryList返回失败');
                }
            })
        },
        selectAjax: function(data) {
            curPage = 1;
            data.pageNum = curPage;
            data.pageSize = pageSize;
            $.ajax({
                url: wsCommon.host + '/api/goods/list/' + wsCommon.getUrlParam('catId'),
                dataType: wsCommon.jsonType,
                data: data,
                success: function(res) {
                    var productsList = res.data.pager.list;
                    var pager = res.data.pager;
                    wsList.randerList(productsList, pager);

                    // 初始化分页

                    wsList.initPaging(pager.pages, pager.total, res)

                },
                error: function() {
                    // console.log('error');
                }
            })
        },
        randerList: function(productsList, pager) {
            // 数量
            $('.op-search-result').html(pager.total);
            //productsList（大图）
            var productsListTpl1 = '';
            $.each(productsList, function(index, item) {
                productsListTpl1 += '<li class="goods-item" style="height: 346px; width: 227px;">\
                                        <div class="goods-pic" style="width:100%">\
                                            <a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank" title="'+item.name+'">\
                                                <img src="'+item.defaultImage.surl+'" class="action-goods-img" alt="'+item.name+'" style="min-width:160px;max-width:220px">\
                                            </a>\
                                        </div>\
                                        <div class="goods-info">\
                                            <h3 class="goods-name" style="height: 36px;overflow:hidden;"><a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank" title="">'+item.name+'&nbsp;<span style="color:red" title="'+item.brief+'">'+(item.brief ? item.brief : "")+'<span></span></span></a></h3>\
                                            <div class="goods-price">\
                                                 <ins class="price">¥'+item.price+'</ins>';
                if(item.mktprice != 0) {
                    productsListTpl1 += '<span style="text-decoration:line-through">原价 ¥'+item.mktprice+'</span>';
                }

                var addShopCartTpl = '';
                if(item.multiSpec) {
                    if(item.totalStore > 0) {
                        addShopCartTpl = '<span class="p-quantity">\
                                               <a href="javascript:void(0);" class="btn-decrease">-</a><input type="text"  class="action-quantity-input" value="1" data-total="'+item.store+'"><a href="javascript:void(0);" class="btn-increase" data-total="'+item.store+'">+</a>\
                                            </span>\
                                            <a class="btn btn-major action-addtocart  ext-buy" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank"><span><span>立即购买</span></span></a>';
                    } else {
                        addShopCartTpl = '<a class="btn btn-major action-addtocart ext-non" href="javascript:void(0);" data-role="noticeShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-name="'+item.name+'" data-img="'+item.defaultImage.imageId+'"><span><span>到货通知</span></span></a>';
                    }

                }else {
                    if(item.store > 0) {
                        addShopCartTpl += '<span class="p-quantity">\
                           <a href="javascript:void(0);" class="btn-decrease">-</a><input type="text"  class="action-quantity-input" value="1" data-total="'+item.store+'"><a href="javascript:void(0);" class="btn-increase" data-total="'+item.store+'">+</a>\
                        </span>\
                        <a class="btn btn-major action-addtocart" href="javascript:void(0);" data-role="addShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-objtype="'+item.objType+'"><span><span>加入购物车</span></span></a>';
                    } else {
                        addShopCartTpl += '<a class="btn btn-major action-addtocart ext-non" href="javascript:void(0);" data-role="noticeShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-name="'+item.name+'" data-img="'+item.defaultImage.imageId+'"><span><span>到货通知</span></span></a>';
                    }
                }

                productsListTpl1 += '</div>\
                    </div>\
                    <div class="promotion-tags">\
                        <span class="tag">减价</span>已有<a href="javascript:;">800561</a>人评价\
                    </div>\
                    <div class="goods-action">\
                        <div class="goods-buy">';
                    productsListTpl1 += addShopCartTpl;

                    productsListTpl1 += '</div>\
                                        </div>\
                                      </li>';
            })
            $('[data-role="productsListRow"]').html(productsListTpl1);

            //productsList（列表）
            var productsListTpl2 = '';

            $.each(productsList, function(index, item) {
                var addShopCartTpl1 = '';
                if(item.multiSpec) {
                    if(item.totalStore > 0) {
                        addShopCartTpl1 = '<span class="p-quantity">\
                                               <a href="javascript:void(0);" class="btn-decrease">-</a><input type="text"  class="action-quantity-input" value="1" data-total="'+item.store+'"><a href="javascript:void(0);" class="btn-increase" data-total="'+item.store+'">+</a>\
                                            </span>\
                                            <a class="btn btn-major action-addtocart ext-buy" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank"><span><span>立即购买</span></span></a>';
                    } else {
                        addShopCartTpl1 = '<a class="btn btn-major action-addtocart ext-non" href="javascript:void(0);" data-role="noticeShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-name="'+item.name+'" data-img="'+item.defaultImage.imageId+'"><span><span>到货通知</span></span></a>';
                    }

                }else {
                    if(item.store > 0) {
                        addShopCartTpl1 += '<span class="p-quantity">\
                           <a href="javascript:void(0);" class="btn-decrease">-</a><input type="text"  class="action-quantity-input" value="1" data-total="'+item.store+'"><a href="javascript:void(0);" class="btn-increase" data-total="'+item.store+'">+</a>\
                        </span>\
                        <a class="btn btn-major action-addtocart" href="javascript:void(0);" data-role="addShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-objtype="'+item.objType+'"><span><span>加入购物车</span></span></a>';
                    } else {
                        addShopCartTpl1 += '<a class="btn btn-major action-addtocart ext-non" href="javascript:void(0);" data-role="noticeShopCart" data-price="'+item.price+'" data-goodsid="'+item.goodsId+'" data-productid="'+item.productId+'" data-name="'+item.name+'" data-img="'+item.defaultImage.imageId+'"><span><span>到货通知</span></span></a>';
                    }
                }
                productsListTpl2 += '<li class="goods-item clearfix">\
                                       <div class="goods-pic">\
                                           <a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank"><img class="action-goods-img" alt="'+item.name+'" src="'+item.defaultImage.surl+'"></a>\
                                       </div>\
                                       <div class="goods-action">\
                                           <div class="goods-buy">';
               productsListTpl2 += addShopCartTpl1
                productsListTpl2 += '</div>\
                               </div>\
                               <div class="goods-price">\
                                   <ul>\
                                      <li><label for="">销售价：</label><ins class="price">¥'+item.price+'</ins></li>\
                                   </ul>\
                               </div>\
                               <div class="goods-info">\
                                   <h3 class="goods-name"><a href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'" target="_blank">'+item.name+'&nbsp;<span style="color:red" title="'+item.brief+'">'+(item.brief ? item.brief : "")+'<span></span></span></a></h3>\
                               </div>\
                           </li>';
            })
            $('.gallery-list ul').html(productsListTpl2);
        },
        // selectChangeData: function() {
            // $.ajax({
            //     url: wsCommon.host + '/api/goods/list/' + wsCommon.getUrlParam('catId'),
            //     dataType: wsCommon.jsonType,
            //     success: function(res) {
            //
            //     },
            //     error: function() {
                    // console.log('queryList返回失败1');
            //     }
            // })
        // },
        // 分页
        getPageDates: function() {
            var self = this;
            var totalData = selectData(brangIdArr, propsArr);

            totalData.pageNum = curPage;
            totalData.pageSize = pageSize;
            totalData.hasGoods = selectItemParamHasGoods;
            $.ajax({
                url: wsCommon.host + '/api/goods/list/' + wsCommon.getUrlParam('catId'),
                dataType: wsCommon.jsonType,
                data: totalData,
                success: function(res) {

                    // 同步页码
                    var totalPage = res.data.pager.pages;
                    sPage.find('.page-count').html('<span class="page-current">'+curPage+'</span>/<span class="page-total">'+totalPage+'</span>');
                    nextBtn.removeClass('over');
                    prevBtn.removeClass('over');
                    if(curPage >= totalPage) {
                        nextBtn.addClass('over');
                    }
                    if(curPage <= 1) {
                        prevBtn.addClass('over');
                    }

                    var pageData = res.data.pager.list;
                    var pager = res.data.pager;
                    self.randerList(pageData, pager);
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
        // pageselectCallback: function(page_index) {
        //     if(page_index == 0) {
        //
        //     } else {
        //         curPage = page_index + 1;
        //         wsList.getPageDates();
        //     }
        //
        // },
        initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;
                                sPage.find('.page-count').html('<span class="page-current">'+curPage+'</span>/<span class="page-total">'+totalPage+'</span>');
                                nextBtn.removeClass('over');
                                prevBtn.removeClass('over');
                                if(curPage >= totalPage) {
                                    nextBtn.addClass('over');
                                }
                                if(curPage <= 1) {
                                    prevBtn.addClass('over');
                                }

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                wsList.randerList(pageData, pager);
                            } else {
                                curPage = page_index + 1;
                                wsList.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                    // 迷你分页
                    sPage.show();
                    sPage.find('.page-count').html('<span class="page-current">'+curPage+'</span>/<span class="page-total">'+totalPage+'</span>');
                }else {
                    sPage.hide();
                    ele.hide();
                    $('[data-role="productsListCol"]').html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>')
                    productContainer.html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>');
                }

            prevBtn.off('.prev');
            nextBtn.off('.next');
            prevBtn.on('click.prev', function(evt) {
                if($(this).hasClass('over')) return;
                nextBtn.removeClass('over');
                prevPage(evt);
                var num = getCurrentNum();
                if(num <= 1) {
                    prevBtn.addClass('over');
                    nextBtn.removeClass('over');
                }
                sPage.find('.page-count').html('<span class="page-current">'+getCurrentNum()+'</span>/<span class="page-total">'+totalPage+'</span>');
            });
            nextBtn.on('click.next', function(evt) {
                if($(this).hasClass('over')) return;
                prevBtn.removeClass('over');
                nextPage(evt);

                var num = curPage;
                if(num >= totalPage) {
                    prevBtn.removeClass('over');
                    nextBtn.addClass('over');
                }
                sPage.find('.page-count').html('<span class="page-current">'+getCurrentNum()+'</span>/<span class="page-total">'+totalPage+'</span>');
            });
        },
        // 到货通知
        arrivalNotice: function() {
            //弹层
    		$(document).on('click','[data-role="noticeShopCart"]' ,function(){
    		   $(".cover").show();
    		   $("#notice_pop").show();

    		})
    		$(document).on('click', ".popup-btn-close", function(){
    		   $(this).parents(".popup-container").hide();
    		   $(".cover").hide();
    		})

            // 提交
            $(document).on('click', '[data-role="arrivalNotice"]', function() {
                var email = $('[data-arrivalNotice="email"]').val().trim();
                var phone = $('[data-arrivalNotice="phone"]').val().trim();
                var phonereg = /^((13[0-9])|(15[^4,\D])|(18[0,3-9]))\d{8}$/;
                var emailreg = /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
                if(email == '' && phone == '') {
                    iosOverlay({
                        text: "邮箱地址和手机号码至少填写一个!",
                        duration: 1500
                    });
                    return;
                } else if(!emailreg.test(email) && email != '') {
                    iosOverlay({
                		text: "邮箱格式不正确!",
                        duration: 1500
                	});
                    return;
                } else if(!phonereg.test(phone) && phone != '') {
                    iosOverlay({
                		text: "手机号码格式不正确!",
                        duration: 1500
                	});
                    return;
                } else {
                    var noticeDom = $('[data-role="noticeShopCart"]');
                    $.ajax({
                        url: wsCommon.host + '/api/memberGoods/save',
                        dataType: wsCommon.jsonType,
                        data: {
                            goodsId: noticeDom.attr('data-goodsid'),
                            productId: noticeDom.attr('data-productid'),
                            goodsName: noticeDom.attr('data-name'),
                            goodsPrice: noticeDom.attr('data-price'),
                            imageDefaultId: noticeDom.attr('data-img'),
                            email: email,
                            cellphone: phone
                        },
                        success: function(res) {
                            if(res.code == 1) {
                                iosOverlay({
                                    text: res.reason,
                                    duration: 1500
                                });
                                $(".cover").hide();
                     		    $("#notice_pop").hide();
                            }
                        },
                        error: function() {
                            // console.log('购物车显示接口返回失败');
                        }
                    })
                }
            })
        },
        selectItemAjax: function(data) {
            curPage = 1;
            data.pageNum = curPage;
            data.pageSize = pageSize;
            data.hasGoods = selectItemParamHasGoods;

            for(i in selectItemParam) {
                data[i] = selectItemParam[i];
            }
            $.ajax({
                url: wsCommon.host + '/api/goods/list/' + wsCommon.getUrlParam('catId'),
                dataType: wsCommon.jsonType,
                data: data,
                success: function(res) {
                    var productsList = res.data.pager.list;
                    var pager = res.data.pager;
                    wsList.randerList(productsList, pager);

                    // 初始化分页

                    wsList.initPaging(pager.pages, pager.total, res)

                },
                error: function() {
                    // console.log('error');
                }
            })
        },
        selectItem: function() {
            // 有无货
            $('.action-available').on('click', function() {
                if($(this).is(':checked')) {
                    selectItemParamHasGoods = true;
                    wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                } else {
                    selectItemParamHasGoods = false;
                    wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                }
            })

            $('.action-sort').on('click', function() {
                var $this = $(this);
                var index = $this.index();

                if($this.hasClass("active")){
				   if($this.attr("id")=="price_sort"){
					    if($this.hasClass("price-desc")){
							$this.addClass("price-asc").removeClass("price-desc");
							$this.attr("data-sort","price_asc");
                            selectItemParam = {
                                priceSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
						}else{
							$this.addClass("price-desc").removeClass("price-asc");
							$this.attr("data-sort","price_desc");
                            selectItemParam = {
                                priceSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
						}
					}
                    if($this.attr("id")=="sell_sort"){
 					    if($this.hasClass("sell-desc")){
 							$this.addClass("sell-asc").removeClass("sell-desc");
 							$this.attr("data-sort","sell_asc");
                            selectItemParam = {
                                buyCountSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
 						}else{
 							$this.addClass("sell-desc").removeClass("sell-asc");
 							$this.attr("data-sort","sell_desc");
                            selectItemParam = {
                                buyCountSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
 						}
 					}
                    if($this.attr("id")=="comment_sort"){
 					    if($this.hasClass("comment-desc")){
 							$this.addClass("comment-asc").removeClass("comment-desc");
 							$this.attr("data-sort","comment_asc");
                            selectItemParam = {
                                commentsCountSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
 						}else{
 							$this.addClass("comment-desc").removeClass("comment-asc");
 							$this.attr("data-sort","comment_desc");
                            selectItemParam = {
                                commentsCountSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
 						}
 					}
				}else{
                    if(index == 0) {
                        selectItemParam = {};
                        wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                    } else if(index == 1) {
                        if($this.attr('data-sort') == 'action_desc') {
                            selectItemParam = {
                                buyCountSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        } else {
                            selectItemParam = {
                                buyCountSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        }
                    } else if(index == 2) {
                        if($this.attr('data-sort') == 'price_desc') {
                            selectItemParam = {
                                priceSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        } else {
                            selectItemParam = {
                                priceSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        }
                    } else if(index == 3) {
                        if($this.attr('data-sort') == 'comment_desc') {
                            selectItemParam = {
                                commentsCountSort: -1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        } else {
                            selectItemParam = {
                                commentsCountSort: 1
                            };
                            wsList.selectItemAjax(selectData(brangIdArr, propsArr));
                        }
                    }

					$this.addClass("active").siblings(".action-sort").removeClass("active");
				}

            })


        },
        weekRank: function() {
            $.ajax({
                url: wsCommon.host + '/api/goods/listBuyWCountRank',
                dataType: wsCommon.jsonType,
                data: {
                    catId: wsCommon.getUrlParam('catId')
                },
                success: function(res) {
                    var list = res.data.pager.list;
                    var tpl = '';
                    $.each(list, function(index, item) {
                        tpl += '<li class="item clearfix  selected">\
                                 <div class="im"><a target="_blank" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'"><img src="'+(item.defaultImage && item.defaultImage.surl)+'" alt="'+item.name+'" width="60" height="60"></a><span class="hl">'+(index+1)+'</span></div>\
                                 <div class="rank-box clearfix">\
                                     <div class="gname"><h6><a title="" target="_blank" href="./info.html?goodsId='+item.goodsId+'&catId='+item.catId+'">'+item.name+'</a></h6></div>\
                                     <ul class="p"><li><span class="price1">¥'+item.price+'</span></li></ul>\
                                 </div>\
                            </li>';
                    })
                    $('[data-role="ranklist"]').html(tpl);
                },
                error: function() {
                    // console.log('error');
                }
            })
        },
        init: function() {
            this.getList();
            this.arrivalNotice();
            this.selectItem();
            this.weekRank();
        }
    }
    wsList.init();
})
